//
//  YLInvitingFriendsViewModel.m
//  YLFinanceService
//
//  Created by she1323 on 2017/12/6.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLInvitingFriendsViewModel.h"

#import "WHNetworkTool.h"
#import "WHProgressHUD.h"
#import "YLAccountTool.h"
#import <YYModel.h>
#import "YLInvitingFriendsModel.h"
@implementation YLInvitingFriendsViewModel

- (RACCommand *)requestCommand
{
    if (!_requestCommand) {
        __weak typeof(self) weakSelf = self;
        _requestCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
               
                [WHProgressHUD showStatus:@"正在获取我的邀请列表..."];
                NSString *url = [NSString stringWithFormat:@"%@AppCommon/PGetInvitationVoucherLog", YLURL];
                NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:2];
                params[@"PageIndex"] = @(1);
                params[@"MaxResultCount"] = @(65534);
                
                [[WHNetworkTool shareInstance] sendPostWithUrl:url header:[YLAccountTool header] params:params success:^(id responseObject) {
                    [WHProgressHUD dismiss];
                    if ([responseObject[@"httpCode"] integerValue] == 200) {
                        
                        YLInvitingFriendsItem *item = [YLInvitingFriendsItem yy_modelWithDictionary:responseObject[@"httpResult"]];
                        
                        
                        weakSelf.item = item;
                        
                        [subscriber sendNext:nil];
                        
                        
                    } else {
                        [WHProgressHUD showError:responseObject[@"message"]];
                    }
                    
                    [subscriber sendCompleted];
                    
                } fail:^(NSError *error) {
                    [subscriber sendCompleted];
                }];
                
                return nil;
            }];
            
        }];
    }
    return _requestCommand;
}

- (void)dealloc
{
    [[WHNetworkTool shareInstance] cancel];
}

@end
